﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Mvc;

namespace SentientGrid
{
    class GridBuilder
    {
        private Type Type { get; set; }
        private string Id { get; set; }
        public string Label { get; set; }
        private string Url { get; set; }
        private IDictionary<string, object> GridParameters { get; set; }
        private ModelMetadata MetaData { get; set; }
        private AttributeParser AttributeParser { get; set; }

        public GridBuilder(string mId, string mLabel, Type mType, string mUrl, ModelMetadata mMetaData, IDictionary<string, object> mGridParameters = null)
        {
            Id = mId;
            Label = mLabel;
            Type = mType;
            Url = mUrl;
            GridParameters = mGridParameters;
            MetaData = mMetaData;
            AttributeParser = new AttributeParser(Type);
        }
        protected internal string BuildJQGrid()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script type=\"text/javascript\">");
            sb.Append("$(document).ready(function() {");
            sb.AppendFormat("$('#{0}').jqGrid({{", Id);
            sb.AppendFormat("url:'{0}', datatype:'json',mtype:'GET',caption:'{1}',height:'100%',", Url, Label ?? Id);
            sb.Append(BuildColNames());
            sb.Append(BuildColModel());
            sb.Append(BuildParameters());
            sb.RemoveLast();
            sb.Append("})});</script>");

            return sb.ToString();

        }

        private string BuildParameters()
        {
            var sb = new StringBuilder();
            if (GridParameters != null)
            {
                foreach (var item in GridParameters)
                {
                    sb.AppendFormat("{0}:'{1}',", item.Key, item.Value);
                }
            }
            return sb.ToString();
        }

        private string BuildColModel()
        {
            var colWidths =  AttributeParser.GetGridColumnWidthAttributeValues();
            var colEditable = AttributeParser.GetGridColumnEditableAttributeValues();
            var colFormatter = AttributeParser.GetGridColumnFormatterAttributeValues();
            var colCustom = AttributeParser.GetGridColumnCustomAttributeValues();

            var sb = new StringBuilder();
            sb.Append("colModel:[");
            foreach (var item in AttributeParser.GetDisplayedProperties())
            {
                var fmt = colFormatter[item.Name];
                if (string.IsNullOrEmpty(fmt.ToString()))
                {
                    sb.AppendFormat("{{name:'{0}', index:'{0}', width:{1},editable:{2},{3}}},", item.Name, colWidths[item.Name], colEditable[item.Name], colCustom[item.Name]);
                }
                else
                {
                    sb.AppendFormat("{{name:'{0}', index:'{0}', width:{1},editable:{2},{3},{4}}},", item.Name, colWidths[item.Name], colEditable[item.Name], fmt, colCustom[item.Name]);
                }
            }
            sb.RemoveLast();
            sb.Append("],");
            return sb.ToString();
        }

        private string BuildColNames()
        {

            var sb = new StringBuilder();
            sb.Append("colNames:[");
            var dictionary = AttributeParser.GetDisplayNameAttributeValues();
            foreach (var item in AttributeParser.GetDisplayedProperties())
            {
                
                sb.AppendFormat("'{0}',", dictionary[item.Name]);
            }
            sb.RemoveLast();
            sb.Append("],");

            return sb.ToString();
        }

        
    }
}
